package com.yiren.manager.ctrl.test;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.type.TypeReference;
import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.FavoriteSet;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrElder;
import com.yiren.entity.YrElderTestDiseaseRecord;
import com.yiren.entity.YrElderTestResultRecord;
import com.yiren.entity.YrTestTopic;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.test.YrElderTestDiseaseRecordForm;
import com.yiren.manager.form.test.YrElderTestResultRecordForm;
import com.yiren.manager.service.iface.elder.YrElderService;
import com.yiren.manager.service.iface.test.YrElderTestDiseaseRecordService;
import com.yiren.manager.service.iface.test.YrElderTestResultRecordService;
import com.yiren.manager.service.iface.test.YrTestTopicService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("elderTestDiseaseRecord")
@Controller
public class YrElderTestDiseaseRecordController extends GenericController<YrElderTestDiseaseRecordForm, YrElderTestDiseaseRecord>{

	@Autowired
	private YrElderTestDiseaseRecordService selfService;
	
	@Autowired
	private YrElderTestResultRecordService elderTestResultRecordService;
	
	@Autowired
	private YrTestTopicService testTopicService;
	
	@Autowired
	private YrElderService elderService;
	
	
	@RequestMapping("saveBatch")
	@ResponseBody
	public HTTPResult<YrElderTestDiseaseRecordForm> saveBatch(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrElderTestDiseaseRecordForm> result = new HTTPResult<YrElderTestDiseaseRecordForm>();
		
		JsonMapperTool tool = new JsonMapperTool(data);
		System.out.println(data.toString());
		
		ArrayList<YrElderTestDiseaseRecordForm> elderTestDiseaseRecordForms = tool.mapper("elderTestDiseaseRecords", 
				new TypeReference<ArrayList<YrElderTestDiseaseRecordForm>>() {});
		
		YrElderTestResultRecordForm testResultRecordForm = tool.mapper("testResultRecord", YrElderTestResultRecordForm.class);
//		if(null==elderTestDiseaseRecordForms||null==testResultRecordForm){
//			throwExp("参数有误");
//		}
		
		String sqlWhere = "elder_id=?1 AND test_topic_id=?2 AND test_times_number=?3";
		QueryResult<YrElderTestResultRecord> qr = elderTestResultRecordService.find(YrElderTestResultRecord.class, sqlWhere, new Object[]{testResultRecordForm.getElderId(),testResultRecordForm.getTestTopicId(),testResultRecordForm.getTestTimesNumber()});
		if(qr.getTotalRecord()>0){
			throwExp("该老者已测试过该题目，请勿重复测试");
		}
		
		Date now = new Date();
		List<YrElderTestDiseaseRecord> entityList = new ArrayList<YrElderTestDiseaseRecord>();
		if(elderTestDiseaseRecordForms!=null){
			for(YrElderTestDiseaseRecordForm form : elderTestDiseaseRecordForms){
				YrElderTestDiseaseRecord entity = ObjectUtils.objectToObject(form, YrElderTestDiseaseRecord.class);
				entity.setDiseaseRecordId(UUIDGenerator.getUUID());
				entity.setCreateTime(now);
				entityList.add(entity);
			}
		}
		
		YrElderTestResultRecord elderTestResultRecord = ObjectUtils.objectToObject(testResultRecordForm, YrElderTestResultRecord.class);
		elderTestResultRecord.setResultRecordId(UUIDGenerator.getUUID());
		elderTestResultRecord.setTestResultItemId("-1");
		elderTestResultRecord.setCreateTime(now);
			
		FavoriteSet favoriteSet = new FavoriteSet();
		favoriteSet.putFavorite("elderTestDiseaseRecords", entityList);
		favoriteSet.putFavorite("testResultRecord", elderTestResultRecord);
		
		selfService.saveBatch(favoriteSet);
		
		return result;
	}
	
	@RequestMapping("findElderTestResult")
	@ResponseBody
	public HTTPResult<YrElderTestDiseaseRecordForm> findElderTestResult(String elderId,String testTopicId,Integer testTimesNumber) throws Exception{
		HTTPResult<YrElderTestDiseaseRecordForm> result = new HTTPResult<YrElderTestDiseaseRecordForm>();
		if(null==elderId||null==testTopicId||testTimesNumber==null){
			throwExp("参数有误");
		}
		
		YrElder elder = elderService.findOne(elderId);
		if(null==elder){
			throwExp("查不到相关老者信息");
		}

		YrTestTopic testTopic = testTopicService.findOne(testTopicId);
		if(null==testTopic){
			throwExp("查不到题目信息");
		}
	
		String sqlWhere = "elder_id=?1 AND test_topic_id=?2 AND test_times_number=?3";
		QueryResult<YrElderTestResultRecord> qrElderTestResultRecord = elderTestResultRecordService.find(YrElderTestResultRecord.class, sqlWhere, new Object[]{elderId,testTopicId,testTimesNumber});
		if(qrElderTestResultRecord.getTotalRecord()==0){
			throwExp("该老者尚未测试此题");
		}
		
		List<YrElderTestDiseaseRecordForm> formList = new ArrayList<YrElderTestDiseaseRecordForm>();
		
		QueryResult<YrElderTestDiseaseRecord> qr = selfService.find(YrElderTestDiseaseRecord.class, sqlWhere, new Object[]{elderId,testTopicId,testTimesNumber});
		
		for(YrElderTestDiseaseRecord entity : qr.getResultList()){
			formList.add(toForm(entity));
		}
		
		result.setData(formList);
		return result;
	}
	
	
	@Override
	public YrElderTestDiseaseRecordForm toForm(YrElderTestDiseaseRecord e) throws Exception {
		YrElderTestDiseaseRecordForm form = ObjectUtils.objectToObject(e, YrElderTestDiseaseRecordForm.class);
		return form;
	}

	@Override
	public YrElderTestDiseaseRecord toEntity(YrElderTestDiseaseRecordForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
